package pub.guoxin.oops.manager.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pub.guoxin.oops.annotation.InjectUser;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.manager.service.AlarmDisposeService;
import pub.guoxin.oops.manager.vo.AlarmDisposeVo;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

/**
 * Create by guoxin on 2018/5/5
 */
@RestController
@RequestMapping(OopsPathConstants.ALARM_DISPOSE_PATH)
public class AlarmDisposeController {

    @Autowired
    private ResultVoBuilder builder;
    @Autowired
    private AlarmDisposeService alarmDisposeService;

    @PreAuthorize(value = "hasAnyRole('ADMIN, USER')")
    @ApiOperation(value = "预警处理分页", notes = "预警处理分页（权限：公司管理员）")
    @GetMapping(value = "/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo register(@InjectUser User user,
                             @RequestParam(required = false) String groupId,
                             @RequestParam(required = false) String deviceSn,
                             @RequestParam(required = false) String fieldName,
                             @RequestParam(required = false, defaultValue = "0") Integer pageNumber,
                             @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<AlarmDisposeVo> page = alarmDisposeService.page(user, groupId, deviceSn, fieldName, pageNumber, pageSize);
        return builder.buildSuccessResultInfo(page);
    }

    @PreAuthorize(value = "hasAnyRole('ADMIN, USER')")
    @ApiOperation(value = "预警处理", notes = "预警处理分页（权限：公司管理员）")
    @PostMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo dispose(@InjectUser User user, @PathVariable String id, String remark) {
        boolean result = alarmDisposeService.remark(user, id, remark);
        if (result)
            return builder.buildSuccessResultInfo();
        else
            return builder.buildBadRequestResultInfo();
    }

}
